home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / fp / ifp_unix.lzh / ifp / interp / struct.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-23  |  15.7 KB  |  513 lines

  1.  
  2. /****** struct.h ******************************************************/
  3. /**                                                                  **/
  4. /**                    University of Illinois                        **/
  5. /**                                                                  **/
  6. /**                Department of Computer Science                    **/
  7. /**                                                                  **/
  8. /**   Tool: IFP                         Version: 0.6                 **/
  9. /**                                                                  **/
  10. /**   Author:  Arch D. Robison          Date:   May 1, 1985          **/
  11. /**                                                                  **/
  12. /**   Revised by: Arch D. Robison       Date:  May 23, 1989          **/
  13. /**                                                                  **/
  14. /**   Principal Investigators: Prof. R. H. Campbell                  **/
  15. /**                            Prof. W. J. Kubitz                    **/
  16. /**                                                                  **/
  17. /**                                                                  **/
  18. /**------------------------------------------------------------------**/
  19. /**   (C) Copyright 1989  University of Illinois Board of Trustees   **/
  20. /**                       All Rights Reserved.                       **/
  21. /**********************************************************************/
  22.  
  23. /*
  24.  * There are some preprocessor variables which must be defined either
  25.  * here or in the cc command.  The following options are not available
  26.  * in the release version:
  27.  *
  28.  *     ARRAYS, COMPILE, UMAX, VECTOR, OOFP, OUTBERKELEY
  29.  *
  30.  * In the release version, some of the code for these options is 
  31.  * removed from the source by unifdef(1), so the source may look strange 
  32.  * in places.  (E.g. degenerate switch statements).
  33.  *
  34.  * The preprocessor variables are listed below.
  35.  *
  36.  * OPSYS (UNIX, MSDOS, CTSS) - specifies operating system
  37.  * PCAT - for compiling on PC/ATs
  38.  * SQUEEZE - put space at a premium
  39.  * ARRAYS - incorporate array representation of sequences
  40.  * COMPILE - incorporate IFP compiler (see C_comp.h)
  41.  * DEBUG - incorporate interpreter debugging spy points
  42.  * DUMP - incoporate dump command for debugging (see debug.c)
  43.  * OOFP - object-oriented FP (incompatible with IFP)
  44.  * REFCHECK - incorporate reference checking command (see apply.c)
  45.  * UMAX - make parallel version for Encore Multimax
  46.  * VECTOR - define APL-style vector operations (must define ARRAYS also)
  47.  *
  48.  * There are also preprocessor variables which may be turned on or off
  49.  * in the following files:
  50.  *
  51.  *      ECACHE in cache.h - implement expression cache
  52.  *      STATS in stats.h - collect run time statistics
  53.  *      FETCH in node.h - implement "fetch" functional form
  54.  *    OUTBERKELEY in outberkely.h - implement routine to print functions in
  55.  *                      Berkeley FP format.
  56.  *
  57.  * WARNING: Some of the compiling options may interfere with each other.
  58.  *          Some options have not been tested for many revisions, so
  59.  *        new bugs may creep out of the woodwork!
  60.  */
  61.  
  62. #define UMAX 0        /* Must not enable ARRAYS, ECACHE, or STATS if set */
  63. #define DUMP 0
  64. #define ARRAYS 0    /* Must also define VECTOR=1 if set */
  65. #define VECTOR 0
  66. #define DEBUG 0
  67. #define REFCHECK 0    /* Must not enable OOFP if set */
  68. #define OOFP 0
  69.  
  70. /*
  71.  * Possible values for OPSYS preprocessor variable.
  72.  */
  73. #define UNIX  10
  74. #define MSDOS 11
  75. #define CTSS  12
  76. #define APPLE 13
  77.  
  78. #define OPSYS UNIX
  79.  
  80. #if OPSYS==CTSS
  81. /*
  82.  * PARAMBUG is defined to indicate that the C compiler can not
  83.  * take the address (&) of parameter variables correctly.
  84.  * When this bug is removed from the CRAY C compiler, this define
  85.  * and dependent code should be removed.
  86.  */
  87. #define PARAMBUG 1
  88. #endif
  89.  
  90. #if OPSYS==MSDOS || OPSYS==CTSS 
  91. #define MAXPATH 65     /* Maximum pathname length allowed (in characters) */
  92. #endif
  93.  
  94. #if OPSYS==UNIX || OPSYS==APPLE
  95. #define MAXPATH 256    /* Maximum pathname length allowed (in characters) */
  96. #endif
  97.  
  98. #if OPSYS==CTSS||OPSYS==APPLE
  99. #define index strchr
  100. #endif
  101.  
  102. #ifdef PCAT
  103. #define index strchr
  104. #endif
  105.  
  106. /********** Fundamental Data Structures and Constants **********/
  107.  
  108.  
  109. #define private static
  110. #define forward extern  /* for forward definitions which are not external */
  111.  
  112. typedef int boolean;
  113. typedef long FPint;
  114. typedef int FPboolean;
  115. typedef short ushort;
  116.  
  117. /********************** MACHINE DEPENDENT CONSTANTS **********************/
  118.  
  119. /* These two definitions assume two's complement arithmetic! */
  120. #define FPMaxInt (((FPint) 1 << 8 * sizeof(FPint) - 1) - 1)
  121. #define MaxInt   (((  int) 1 << 8 * sizeof(  int) - 1) - 1)
  122. #define FPMinInt (-FPMaxInt-1)
  123.  
  124. #ifdef SQUEEZE
  125.  
  126. /* Maximum floating point value representable by an FPfloat */
  127. typedef float FPfloat;
  128.  
  129. #define MAXFLOAT 1e38
  130. #define LNMAXFLOAT 88.7
  131.  
  132. #define CompTol (1e-6)
  133.  
  134. #else
  135.  
  136. typedef double FPfloat;
  137.  
  138. /* Maximum floating point value representable by an FPfloat */
  139. #define MAXFLOAT 1.8e308
  140. #define LNMAXFLOAT 710.37     /* ln (MAXFLOAT) */
  141.  
  142. #define CompTol (1e-8)
  143.  
  144. #endif
  145.  
  146. /* if abs(A),abs(B) are both < MAXFACTOR then A*B will fit in FPInt */
  147. #define MAXFACTOR 0xB504L
  148.  
  149. /****************** end of machine dependent constants *********************/
  150.  
  151. /********************************* Strings *********************************/
  152.  
  153. /*
  154.  * StrCell
  155.  *
  156.  * Each string is segmented into a linked list.  The first record of the
  157.  * linked list contains the reference count for the string.
  158.  * The string is terminated by a segment with a null StrNext field or
  159.  * a '\0', whichever comes first.  The empty string is represented
  160.  * by a null pointer.  Segments have '\0' as their first character iff
  161.  * they are in the free string list.
  162.  */
  163.  
  164. /*
  165.  * StrHeadLen is the maximum number of characters which can be contained in
  166.  * the first segment of a string list.  
  167.  */
  168. #if OPSYS==CTSS
  169. #define StrHeadLen 8    /* For 64-bit ushort and 64-bit pointer */
  170. #else 
  171. #define StrHeadLen 10    /* For 16-bit ushort and 32-bit pointer */
  172. #endif
  173.  
  174. #define StrTailLen (StrHeadLen + sizeof (ushort))
  175.  
  176. typedef struct StrCell {
  177.    struct StrCell *StrNext;
  178.    union {
  179.       char StrVar1 [StrTailLen];
  180.       struct {
  181.      char StrV1F1 [StrHeadLen];
  182.      ushort StrV1F2;
  183.       } StrVar2;
  184.    } StrUni1;
  185. } StrCell;
  186.  
  187. typedef StrCell *StrPtr;
  188.  
  189. #define StrChar StrUni1.StrVar1
  190. #define SRef StrUni1.StrVar2.StrV1F2
  191.  
  192. /****************************** Sequences ******************************/
  193.  
  194. /*
  195.  * Sequences are guaranteed not to have cycles by the definition of FP.
  196.  * Note that function representation lists may have a cycle, but the cycle
  197.  * will always contain a function name as a member.  Cycle will be broken
  198.  * when the function definition is deleted.
  199.  */
  200.  
  201. /* Object Tags */
  202. #define BOTTOM  0
  203. #define BOOLEAN 1
  204. #define INT     2
  205. #define FLOAT   3
  206. #define LIST    4
  207. #define STRING  5
  208. #define NODE    6
  209. #define CODE    7
  210.  
  211. /* Bitmasks for PairTest */
  212. #define NUMERIC ((1<<FLOAT)|(1<<INT))
  213. #define ATOMIC (NUMERIC | (1<<BOOLEAN) | (1<<STRING))
  214.  
  215.  
  216.  
  217. #define MAXTAG  7
  218. #define SEQUENCE (1<<LIST)
  219.  
  220.  
  221. /* Tag checking expressions dependent upon tag value assignments above */
  222. #define Scalar(Tag) ((Tag) < 4)
  223. #define Numeric(Tag) (((Tag)&~1)^2==0)
  224. #define NotNumPair(Tag1,Tag2) ((((Tag1)^2)|((Tag2)^2))&~1) 
  225. #define IntPair(Tag1,Tag2) ((Tag1+Tag2) == 4)
  226.  
  227. typedef struct CodeCell {
  228.    int (*CodePtr) ();           /* (*CodePtr) (InOut,CodeParam) */
  229.    int CodeParam;
  230. } CodeCell;
  231.  
  232. typedef union {
  233.    FPfloat _Float;
  234.    FPint _Int;
  235.    FPboolean _Bool;
  236.    struct ListCell *_List;
  237.    StrPtr _String;
  238.    struct NodeDesc *_Node;
  239.    CodeCell _Code;
  240. } ObUnion;              
  241.  
  242. #define Float  Data._Float
  243. #define Int    Data._Int
  244. #define Bool   Data._Bool
  245. #define List   Data._List
  246. #define String Data._String
  247. #define Node   Data._Node
  248. #define Code   Data._Code
  249. #define Class  Data._Class
  250.  
  251. /*
  252.  * Note on ARRAYS structures.  Cells with the ARRAY tag use the List field
  253.  * to point to an array descriptor list.  The first element of the list
  254.  * uses the APtr field, subsequent elements use the ADim field.
  255.  */
  256.  
  257. /*
  258.  * Object 
  259.  *
  260.  * An Object is a union which stores an IFP object.  The _LRef field is not 
  261.  * logically part of an * object, but rather part of a ListCell.  We get much 
  262.  * better packing by including it in Object, since it fits in a 32-bit word 
  263.  * along with the Tag field.
  264.  *
  265.  * Likewise, for the UMAX version the _LRefLock field is physically part
  266.  * of Object though it should be part of ListCell.
  267.  *
  268.  * Note that P->Val = Q->Val will transfer the reference count!
  269.  * Normally, P->Val.Tag = Q->Val.Tag, P->Val.Data = Q->Val.Data
  270.  * should be used instead.
  271.  *
  272.  * Fields
  273.  *    Data = representation of object
  274.  *    LRef = reference count, see definition for LRefOne. 
  275.  *    Tag = BOTTOM,BOOLEAN,INT,FLOAT,LIST,STRING,NODE,CODE,ARRAY,JOIN,CLASS
  276.  */  
  277. typedef struct {
  278.    ObUnion Data;
  279.    ushort _LRef;
  280.    char Tag;   
  281. } Object;
  282.  
  283. /*
  284.  * ListCell
  285.  *
  286.  * Sequences are represented as linked lists of objects.  Each ListCell
  287.  * also contains a reference count (hidden in the Object field).  The
  288.  * value stored in the reference count is offset by -1.  The rationale is
  289.  * that reference counts are always compared against one, and comparing
  290.  * against zero is faster on some machines.  
  291.  */ 
  292. typedef struct ListCell {
  293.    Object Val;            /* Value of first element of sequence (CAR) */
  294.    struct ListCell *Next;    /* Pointer tail of sequence (CDR)         */
  295. } ListCell;
  296.  
  297. #define LRef Val._LRef
  298. #define LRefOne 0           /* value of LRef for reference count of 1 */
  299.  
  300.  
  301. /*
  302.  * Most of the code uses subsets of the alphabet for certain types.
  303.  * For example, P,Q, and R are usually ListPtr.
  304.  */
  305. typedef ListCell *ListPtr;    /* e.g. P,Q,R */
  306. typedef ListPtr *MetaPtr;    /* e.g. A,B,C */
  307. typedef Object *ObjectPtr;    /* e.g. X,Y,Z */
  308.  
  309. #define NIL ((ListPtr) NULL)    /* empty list */
  310.  
  311. /******************************* Definitions ******************************/
  312.  
  313. /*
  314.  * DefDesc
  315.  *
  316.  * Fields
  317.  *     DefFlags = subset of {TRACE,RESOLVED}.
  318.  *     DefCode = code for definition - BOTTOM if not resident.
  319.  *
  320.  * Additional fields for OOFP     
  321.  *    DefDomain = pattern representing domain of definition
  322.  *     DefClass = class in which this definition occurs [1]
  323.  *    DefNext = pointer to next DefDesc in list.
  324.  *
  325.  * [1] If we get serious about this, the form should be <{domain function}>
  326.  *     and there should be a list elsewhere pointing to all functions created
  327.  *     by an object.
  328.  */
  329. typedef struct DefDesc {
  330.    char DefFlags;
  331.    Object DefCode;
  332. } DefDesc;
  333.  
  334. typedef DefDesc *DefPtr;
  335.  
  336. #define TRACE  1       /* Print input and output.                      */
  337. #define RESOLVED 4     /* Mark bit used by reference checker           */
  338.  
  339. /*
  340.  * All compiled FP functions have the following form:
  341.  *
  342.  *   void F (InOut,CodeParam)
  343.  *      ObjectPtr InOut;
  344.  *      int CodeParam;
  345.  *      {...};
  346.  *
  347.  * F replaces *InOut with the result of applying F to *InOut.
  348.  * CodeParam is optional.
  349.  */
  350.  
  351.  
  352. /******************************* Modules *******************************/
  353.  
  354. /*
  355.  * Modules are stored as lists of nodes.  Each node has a pointer to
  356.  * its next sibling and its parent node.
  357.  */
  358. typedef struct {                /* Module node descriptor */
  359.    struct NodeDesc *FirstChild;
  360. } ModDesc;
  361.  
  362. /******************************** Imports ******************************/
  363.  
  364. /*
  365.  * Definition nodes are imported with IMPORT nodes.  An import node in a
  366.  * module points to a definition node elsewhere.
  367.  */
  368. typedef struct {
  369.    Object ImpDef;     /* Can be path list or node */
  370. } ImpDesc;
  371.  
  372. /******************************** Nodes ********************************/
  373.  
  374. #define NEWNODE 0 /* Values for NodeType */
  375. #define MODULE 1
  376. #define DEF 2
  377. #define IMPORT 3
  378.  
  379. /*
  380.  * NodeDesc
  381.  *
  382.  * A NodeDesc is a special internal representation for function/PFO paths.
  383.  * To the user, a path always appears as a sequence of strings.  Converting
  384.  * these paths to NodeDesc's saves space and function lookup time.  Also,
  385.  * PFO's represented with NodeDesc are assumed to have correct parameter
  386.  * functions.
  387.  *
  388.  * See the top of node.c for the description of how these are linked together
  389.  * to form the function/module tree.  For OOFP, the NodeDesc structure is
  390.  * quite different, look elsewhere in this file for its definition.
  391.  *
  392.  * NRef = reference count (references by objects)
  393.  * NodeNext = pointer to next sibling (or parent).
  394.  * NodeType = type of node (DEF, MODULE, IMPORT)
  395.  * NodeName = print name of node.
  396.  */
  397. typedef union {
  398.    DefDesc NodeDef;     /* if DEF    */
  399.    ModDesc NodeMod;     /* if MODULE */
  400.    ImpDesc NodeImp;     /* if IMPORT */
  401. } NDunion;
  402.  
  403. typedef struct NodeDesc {
  404.    struct NodeDesc *NodeSib;
  405.    struct NodeDesc *NodeParent;
  406.    StrPtr NodeName;
  407.    short NRef;
  408.    char NodeType;
  409.    NDunion NodeData;
  410. } NodeDesc;
  411.  
  412.  
  413. typedef NodeDesc *NodePtr;
  414.  
  415. /*----------------- exception handling: see except.c -----------------*/
  416.  
  417. /* values for SysError, 0 == no error */
  418.  
  419. #define INTERNAL     1     /* Inexplicable internal error  */
  420. #define NO_LIST_FREE 2       /* Ran out of list cell storage */
  421. #define NO_STR_FREE  3       /*  "   "  " string  "     "    */
  422. #define NO_NODE_FREE 4       /*  "   "  "  node   "     "    */
  423.  
  424. extern short SysError;     /* An error occurred if SysError != 0 */
  425. extern short SysStop;      /* Stop evaluation if != 0            */
  426.  
  427. /*------------ debugging the interpreter: see debug.c ----------------*/
  428.  
  429. /*
  430.  * The interpreter may be compiled with internal spy points.  These spy 
  431.  * points print internal information on stdout.  To include the spy points, 
  432.  * the interpreter must be compiled with #define DEBUG 1.  To turn on a spy 
  433.  * point when running ifp, use the command line option '-d' followed by the 
  434.  * appropriate letters.  The letters are defined by ``DebugOpt'' below.  
  435.  * For example,
  436.  *
  437.  *    ifp -dar
  438.  *
  439.  * will turn on spy points related to memory allocation (a) and 
  440.  * reference counts (r).  Many of these debug options may be irrelevant
  441.  * for the release version of IFP. 
  442.  */
  443. #define DebugParse       (1<<0)    /* parser        */
  444. #define DebugAlloc     (1<<1)    /* memory allocation     */
  445. #define DebugFile    (1<<2)    /* file io         */
  446. #define DebugRef    (1<<3)    /* reference counts     */
  447. #define DebugInit    (1<<4)    /* initialization    */
  448. #define DebugCache    (1<<5)    /* expression cache    */
  449. #define DebugXDef    (1<<6)    /* extended definitions */
  450. #define DebugHyper    (1<<7)    /* hypercube        */
  451. #define DebugUMax    (1<<8)  /* multimax        */
  452. #define DebugSemaphore  (1<<9)  /* semaphores        */
  453. #define DebugFreeList   (1<<10) /* free list        */
  454. #define DebugExpQueue   (1<<11) /* expression queue    */
  455. #define DebugOOFP    (1<<12) /* object oriented FP   */
  456.  
  457. #define DebugOpt "pafricxhusleo"    /* option letters for above */
  458.  
  459. #if DEBUG
  460. extern int Debug;    /* Bit-set of enabled spy points */
  461. #else
  462. #define Debug 0        /* Turn spy points into dead code */
  463. #endif
  464.  
  465. /*--------------------------------------------------------------------*/
  466.  
  467. extern NodePtr CurWorkDir;     /* Current working directory */
  468. extern NodePtr SysDef ();
  469.  
  470. extern void DelLPtr ();         /* Delete a list pointer */
  471. extern ListPtr CopyLPtr ();     /* Copy a list pointer */
  472.  
  473. extern void Rot3 ();            /* list pointer rotation */
  474.  
  475. extern long ListLength ();              /* from list.c */
  476. extern void CopyObject ();
  477. extern ListPtr Repeat  ();
  478. extern boolean RepObject ();
  479. extern void RepLPtr (), NewList ();
  480. extern void CopyTop (), Copy2Top ();
  481. extern void RepTag (),  RepBool ();
  482.  
  483. extern void Apply ();                   /* from apply.c */
  484. extern NodePtr ApplyFun;
  485.  
  486.  
  487. extern void NodeExpand ();
  488.  
  489. extern void ExecEdit (), ReadImport (); /* from file.c */
  490.  
  491. extern void OutObject (), OutList ();   /* from outob.c */
  492. extern void OutString (), OutNode ();
  493. extern void OutForm (), OutFun ();      /* from outfun.c */
  494. extern void OutPretty ();
  495.  
  496. extern void InitIn (), InBlanks ();    /* from inob.c */
  497.  
  498. extern void ReadDef (), DelImport ();
  499. extern void InImport (); 
  500.  
  501. extern int InError();            /* from error.c */
  502. extern void DefError (), IntError ();
  503. extern void FunError (), FormError ();
  504. extern char ArgNotSeq[], ArgObSeq[], ArgSeqOb[], ArgNull[], ArgBottom[];
  505.  
  506. extern NodePtr PrimDef ();
  507. extern char *malloc();
  508.  
  509. #define ArrayEnd(A) (A+(sizeof(A)/sizeof A[0])) 
  510.  
  511.  
  512. /************************** end of struct.h **************************/
  513.